package run.newbug.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import run.newbug.jt.mapper.UserMapper;
import run.newbug.jt.pojo.User;
import run.newbug.jt.vo.PageResult;

import java.util.List;
import java.util.UUID;

@Component
public class UserServiceImpl implements UserService{
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> getAll() {
        return userMapper.selectList(null);
    }

    @Override
    public String login(User user) {
        String pwd = user.getPassword();
        String md5Str = DigestUtils.md5DigestAsHex(pwd.getBytes());
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", user.getUsername())
                .eq("password", md5Str);
//        List<User> users = userMapper.selectList(queryWrapper);
//        if(!users.isEmpty()) return "success";
        User userDB = userMapper.selectOne(queryWrapper);
        String uuid = UUID.randomUUID().toString().replace("-", "");
        return userDB==null ? null : uuid;
    }

    @Override
    public PageResult list(PageResult pageResult) {
        pageResult.setTotal(userMapper.selectCount(null).longValue());
//        userMapper.getUserList(pageResult.getPageNum(),pageResult.getPageSize());
        int startIndex = (pageResult.getPageNum()-1)* pageResult.getPageSize();
        int endIndex = pageResult.getPageSize();
        List<User> rows = userMapper.LimitUsers(startIndex,endIndex);
        pageResult.setRows(rows);
        System.out.println(pageResult);
        return pageResult;
    }

    @Override
    public PageResult list2(PageResult pageResult) {
        IPage iPage = new Page(pageResult.getPageNum(),pageResult.getPageSize());
        Boolean queBoo = StringUtils.hasLength(pageResult.getQuery());
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like(queBoo, "username", pageResult.getQuery());
        IPage iPage1 = userMapper.selectPage(iPage,queryWrapper);
        pageResult.setRows(iPage1.getRecords());
//        pageResult.setTotal(userMapper.selectCount(null).longValue());
        pageResult.setTotal(iPage1.getTotal());
        return pageResult;
    }

    @Override
    public User updaSta(Integer id, Boolean status) {
        User u = new User();
        u.setId(id).setStatus(status);
        userMapper.updateById(u);
        return userMapper.selectById(id);
    }

    @Override
    public List<User> addUSer(User user) {
        String pwd = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        user.setPassword(pwd).setStatus(true);
        userMapper.insert(user);
        return userMapper.selectList(null);
    }

    @Override
    @Transactional
    public List<User> dele(Integer id) {
        userMapper.deleteById(id);
//        int a =1/0;
        return userMapper.selectList(null);
    }

    @Override
    public User updaUserGet(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    public int updaUser(User user) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",user.getId());
        return userMapper.update(user,queryWrapper);
    }

}
